/*
 * TeamsPanel.java
 *
 * Created on 26 marzec 2007, 22:19
 */

package client;

import data.Team;
import data.VacQuota;
import java.rmi.RemoteException;
import java.util.Set;
import javax.swing.JOptionPane;

/**
 *
 * @author  Przemek
 */
public class TeamsPanel extends javax.swing.JPanel
{
    private Set<Team> teams;
    private Team editedTeam;
    
    private VacQuota editedQuota;
    private Team editedTeamWithQuota;
    
    private void fillTeamsList()
    {
        try
        {            
            teams = VacationClient.getInstance().getRemoteServer().getTeams();
            
            jListTeams.setListData( teams.toArray() );
            
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        fillTeamDetails( null );        
    }
    
    private void fillTeamDetails( Team t )
    {
        editedTeam = t;
        if( t == null )
        {            
            jTextTeamName.setText( "" );
        }
        else
        {
            jTextTeamName.setText( t.toString() );
        }
        
        jButtonRemoveTeam.setEnabled( editedTeam != null );
        jButtonSetTeam.setEnabled( editedTeam != null );
    }
    
    private void fillQuotaList()
    {       
        jListQuotas.setEnabled( editedTeam != null );
        jButtonNewQuota.setEnabled( editedTeam != null );
        dateChooserFrom.setEnabled( editedTeam != null );
        dateChooserTo.setEnabled( editedTeam != null );
        jTextFieldLimit.setEnabled( editedTeam != null );
        
        fillQuotaDetails( null );
        
        if( editedTeam == null )
            return;
        
        try
        {            
            editedTeamWithQuota = VacationClient.getInstance().getRemoteServer().reloadTeam( editedTeam, false, true );
            
            Set<VacQuota> quotas = editedTeamWithQuota.getQuotas();

            jListQuotas.setListData( quotas.toArray() );    
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }        

        fillQuotaDetails( null );
    }
    
    private void fillQuotaDetails( VacQuota vq )
    {
        editedQuota = vq;
        
        if( vq == null )
        {
            dateChooserFrom.setDate( null );
            dateChooserTo.setDate( null );
            jTextFieldLimit.setText( "" );
        }
        else
        {
            dateChooserFrom.setDate( vq.getFromDate() );
            dateChooserTo.setDate( vq.getToDate() );
            jTextFieldLimit.setText( Integer.toString( vq.getQuota() ) );
        }
        
        jButtonRemoveQuota.setEnabled( editedQuota != null );
        jButtonSetQuota.setEnabled( editedQuota != null );
    }
    
    private boolean isQuotaCorrect()
    {
        if( dateChooserFrom.getDate() == null )
        {
            JOptionPane.showMessageDialog( this, "From field not filled correctly!", "Error", JOptionPane.ERROR_MESSAGE );
            return false;
        }
        
        if( dateChooserTo.getDate() == null )
        {
            JOptionPane.showMessageDialog( this, "To field not filled correctly!", "Error", JOptionPane.ERROR_MESSAGE );
            return false;
        }
        
        int limit = 0;
        try
        {
            limit = Integer.parseInt( jTextFieldLimit.getText() );
        }
        catch( NumberFormatException e )
        {
            JOptionPane.showMessageDialog( this, "Limit field not filled correctly!", "Error", JOptionPane.ERROR_MESSAGE );
            return false;
        }
        
        if( limit < 0 )
        {
            JOptionPane.showMessageDialog( this, "Limit field value below 0!", "Error", JOptionPane.ERROR_MESSAGE );
            return false;
        }
        
        return true;
    }
    
    
    /** Creates new form TeamsPanel */
    public TeamsPanel()
    {
        initComponents();
        fillTeamsList();
        fillQuotaList();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jListTeams = new javax.swing.JList();
        jTextTeamName = new javax.swing.JTextField();
        jButtonRemoveTeam = new javax.swing.JButton();
        jButtonSetTeam = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListQuotas = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldLimit = new javax.swing.JTextField();
        jButtonNewQuota = new javax.swing.JButton();
        jButtonSetQuota = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButtonRemoveQuota = new javax.swing.JButton();
        dateChooserTo = new datechooser.DateChooserControl();
        dateChooserFrom = new datechooser.DateChooserControl();
        jButtonNewTeam = new javax.swing.JButton();

        setLayout(null);

        setMinimumSize(new java.awt.Dimension(640, 480));
        jListTeams.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListTeams.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListTeamsValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jListTeams);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 20, 110, 180);

        add(jTextTeamName);
        jTextTeamName.setBounds(20, 250, 80, 20);

        jButtonRemoveTeam.setText("Remove");
        jButtonRemoveTeam.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonRemoveTeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveTeamActionPerformed(evt);
            }
        });

        add(jButtonRemoveTeam);
        jButtonRemoveTeam.setBounds(20, 220, 50, 23);

        jButtonSetTeam.setText("Set");
        jButtonSetTeam.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonSetTeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSetTeamActionPerformed(evt);
            }
        });

        add(jButtonSetTeam);
        jButtonSetTeam.setBounds(100, 250, 30, 23);

        jListQuotas.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListQuotasValueChanged(evt);
            }
        });

        jScrollPane2.setViewportView(jListQuotas);

        add(jScrollPane2);
        jScrollPane2.setBounds(170, 20, 220, 110);

        jLabel1.setText("To");
        add(jLabel1);
        jLabel1.setBounds(180, 170, 40, 14);

        jLabel2.setText("Limit");
        add(jLabel2);
        jLabel2.setBounds(180, 200, 50, 14);

        add(jTextFieldLimit);
        jTextFieldLimit.setBounds(230, 200, 160, 20);

        jButtonNewQuota.setText("New");
        jButtonNewQuota.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonNewQuota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewQuotaActionPerformed(evt);
            }
        });

        add(jButtonNewQuota);
        jButtonNewQuota.setBounds(170, 250, 60, 23);

        jButtonSetQuota.setText("Set");
        jButtonSetQuota.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonSetQuota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSetQuotaActionPerformed(evt);
            }
        });

        add(jButtonSetQuota);
        jButtonSetQuota.setBounds(330, 250, 60, 23);

        jLabel3.setText("From");
        add(jLabel3);
        jLabel3.setBounds(180, 140, 40, 14);

        jButtonRemoveQuota.setText("Remove");
        jButtonRemoveQuota.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonRemoveQuota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveQuotaActionPerformed(evt);
            }
        });

        add(jButtonRemoveQuota);
        jButtonRemoveQuota.setBounds(250, 250, 60, 23);

        add(dateChooserTo);
        dateChooserTo.setBounds(230, 170, 160, 20);

        add(dateChooserFrom);
        dateChooserFrom.setBounds(230, 140, 160, 20);

        jButtonNewTeam.setText("New");
        jButtonNewTeam.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonNewTeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewTeamActionPerformed(evt);
            }
        });

        add(jButtonNewTeam);
        jButtonNewTeam.setBounds(80, 220, 50, 23);

    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSetQuotaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSetQuotaActionPerformed
    {//GEN-HEADEREND:event_jButtonSetQuotaActionPerformed
        if( !isQuotaCorrect() )
            return;
        
        editedQuota.setFromDate( dateChooserFrom.getDate() );
        editedQuota.setToDate( dateChooserTo.getDate() );
        editedQuota.setQuota( Integer.parseInt( jTextFieldLimit.getText() ) );
        
        try
        {
            editedQuota = VacationClient.getInstance().getRemoteServer().saveQuota( editedQuota );
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        jListQuotas.setListData( editedTeamWithQuota.getQuotas().toArray() );        
    }//GEN-LAST:event_jButtonSetQuotaActionPerformed

    private void jButtonRemoveQuotaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveQuotaActionPerformed
    {//GEN-HEADEREND:event_jButtonRemoveQuotaActionPerformed
        try
        {
            VacationClient.getInstance().getRemoteServer().delete( editedQuota );
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        editedTeamWithQuota.getQuotas().remove( editedQuota );
        jListQuotas.setListData( editedTeamWithQuota.getQuotas().toArray() );
    }//GEN-LAST:event_jButtonRemoveQuotaActionPerformed

    private void jListQuotasValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListQuotasValueChanged
    {//GEN-HEADEREND:event_jListQuotasValueChanged
        fillQuotaDetails( (VacQuota) jListQuotas.getSelectedValue() );        
    }//GEN-LAST:event_jListQuotasValueChanged

    private void jButtonNewQuotaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonNewQuotaActionPerformed
    {//GEN-HEADEREND:event_jButtonNewQuotaActionPerformed
        if( !isQuotaCorrect() )
            return;
        
        VacQuota newQuota = new VacQuota();
        
        newQuota.setFromDate( dateChooserFrom.getDate() );
        newQuota.setToDate( dateChooserTo.getDate() );
        newQuota.setQuota( Integer.parseInt( jTextFieldLimit.getText() ) );
        
        newQuota.setTeam( editedTeamWithQuota );
        editedTeamWithQuota.getQuotas().add( newQuota );
        try
        {            
            editedTeamWithQuota = VacationClient.getInstance().getRemoteServer().saveTeam( editedTeamWithQuota );
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        jListQuotas.setListData( editedTeamWithQuota.getQuotas().toArray() );
    }//GEN-LAST:event_jButtonNewQuotaActionPerformed

    private void jButtonNewTeamActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonNewTeamActionPerformed
    {//GEN-HEADEREND:event_jButtonNewTeamActionPerformed

        Team newTeam = new Team();
        newTeam.setName( jTextTeamName.getText());
        
        try
        {
            newTeam = VacationClient.getInstance().getRemoteServer().saveTeam( newTeam );
            
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        teams.add( newTeam );
        editedTeam = null;
        jListTeams.setListData( teams.toArray() );        
    }//GEN-LAST:event_jButtonNewTeamActionPerformed

    private void jButtonSetTeamActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSetTeamActionPerformed
    {//GEN-HEADEREND:event_jButtonSetTeamActionPerformed
        editedTeam.setName( jTextTeamName.getText() );
        
        try
        {
            VacationClient.getInstance().getRemoteServer().saveTeam( editedTeam );
            
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        jListTeams.repaint();
    }//GEN-LAST:event_jButtonSetTeamActionPerformed

    private void jButtonRemoveTeamActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveTeamActionPerformed
    {//GEN-HEADEREND:event_jButtonRemoveTeamActionPerformed
        try
        {
            VacationClient.getInstance().getRemoteServer().delete( editedTeam );
            
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        teams.remove( editedTeam );
        editedTeam = null;
        jListTeams.setListData( teams.toArray() );
    }//GEN-LAST:event_jButtonRemoveTeamActionPerformed

    private void jListTeamsValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListTeamsValueChanged
    {//GEN-HEADEREND:event_jListTeamsValueChanged
        fillTeamDetails( (Team) jListTeams.getSelectedValue()  );
        fillQuotaList();
    }//GEN-LAST:event_jListTeamsValueChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private datechooser.DateChooserControl dateChooserFrom;
    private datechooser.DateChooserControl dateChooserTo;
    private javax.swing.JButton jButtonNewQuota;
    private javax.swing.JButton jButtonNewTeam;
    private javax.swing.JButton jButtonRemoveQuota;
    private javax.swing.JButton jButtonRemoveTeam;
    private javax.swing.JButton jButtonSetQuota;
    private javax.swing.JButton jButtonSetTeam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jListQuotas;
    private javax.swing.JList jListTeams;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldLimit;
    private javax.swing.JTextField jTextTeamName;
    // End of variables declaration//GEN-END:variables
    
}
